/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.panchitoboy.shiro.wechat.repository.AuthenticationService;
import com.github.panchitoboy.shiro.wechat.repository.UserInfo;
import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.config.Config;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.config.GlobalConst;
import com.thinkgem.jeesite.modules.bus.entity.UserOpenid;
import com.thinkgem.jeesite.modules.bus.service.UserOpenidService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.UserService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 会员和用户Service
 * @author waile23
 * @version 2017-11-22
 */
@Service("memberUserService")
@Transactional(readOnly = true)
public class MemberUserService implements AuthenticationService {
	@Autowired
	private UserOpenidService userOpenidService;
	@Autowired
	private UserService userService;
	/**
	 * 会员注册
	 * @param memberName
	 * @param loginName
	 * @param genderCode
	 * @param openId
	 * @return
	 */
	@Transactional(readOnly = false)
	@Override
	public UserInfo register(String memberName, String loginName, String genderCode, String openId) {
		/*Member member = memberService.register(StringUtils.EMPTY, memberName, loginName, null, genderCode, GlobalConst.DEFAULT_DATE_TIME.toDate(), "");
		if(member != null) {
	 		UserInfo userInfo = new UserInfo();
	 		userInfo.setId(member.getId());
			userInfo.setName(member.getName());
			userInfo.setPhoneNumber(loginName);
			userInfo.setUserTypeCode(UserTypeEnum.CODE_2.getKey());
			userInfo.setOpenId(openId);
			return userInfo;
		}*/
		return null;
	}
	/**
	 * 通过openid获得用户信息
	 * @param openId
	 * @return
	 */
	public UserInfo getUserInfo(String openId) {
		UserOpenid phoneOpenid = userOpenidService.getByOpenId(openId);
		if(phoneOpenid == null) {
			return null;
		}
		return this.getUserInfoByPhoneNumber(phoneOpenid.getLoginName(), openId);
	
		}
	/**
	 * 通过手机号获得用户信息
	 * @param loginName
	 * @return
	 */
	@Override
	public UserInfo getUserInfoByPhoneNumber(String loginName) {
		String openId = "";
		if (Config.isWechatMode()) {
			UserOpenid phoneOpenid = userOpenidService.getByLoginName(loginName);
			openId = phoneOpenid == null ? "" : phoneOpenid.getOpenid();
		}
		return this.getUserInfoByPhoneNumber(loginName, openId);

	}
	
	/**
	 * 通过和用户和openId获得用户信息
	 * @param loginName
	 * @param openId
	 * @return
	 */
	@Override
	public UserInfo getUserInfoByPhoneNumber(String loginName, String openId) {
		User user = userService.getByLoginName(loginName);
		if(user != null) {
			UserInfo userInfo = new UserInfo();
			userInfo.setId(user.getId());
			userInfo.setName(user.getName());
			userInfo.setPhoneNumber(loginName);
			userInfo.setUserTypeCode(user.getUserType());
			userInfo.setOpenId(openId);
			userInfo.setUsername(user.getLoginName());
			userInfo.setPassword(user.getPassword());
			if(user.getOffice()!=null && StringUtils.isNotBlank(user.getOffice().getId())) {
				userInfo.setOfficeId(user.getOffice().getId());
			}
			userInfo.setLoginFlag(user.getLoginFlag());
			return userInfo;
		}
		return null;
	}
	
	/**
	 * 获得token验证后的用户信息（shiro)
	 * @return
	 */
	public UserInfo getUserInfo() {
		Subject subject = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)subject.getPrincipal();
		return userInfo;
	}
	
	/**
	 * 根据用户类型获取用户列表
	 * 可以在dataScopeFilter = false时处理一些特殊的数据范围
	 * 比如只有查看自己数据权限的用户，可以通过getUserListByUserType(userType, businessOfficeId, false)查询本机构人员信息
	 * @param userType
	 * @param businessOfficeId
	 * @param OfficeScopeFilter 当OfficeScopeFilterTrue时，只查询当前本部门下的用户
	 * @param dataScopeFilter
	 * @return
	 */
	public List<User> getUserListByUserType(String userType, String businessOfficeId, Boolean OfficeScopeFilter, Boolean dataScopeFilter){
		List<User> userList = this.getUserListByUserType(userType, businessOfficeId, dataScopeFilter);
		
		if(!OfficeScopeFilter) {
			return userList;
		}
		
		User user = UserUtils.getUser();
		if(user == null) {
			return userList;
		}
		List<User> newUserList = Lists.newArrayList();
		for(User u : userList) {
			if(u.getOffice().getId().equals(user.getOffice().getId())) {
				newUserList.add(u);
			}
		}
		return newUserList;
	}
	
	/**
	 * 根据用户类型获取用户列表
	 * 可以在dataScopeFilter = false时处理一些特殊的数据范围
	 * 比如只有查看自己数据权限的用户，可以通过getUserListByUserType(userType, businessOfficeId, false)查询本机构人员信息
	 * @param userType
	 * @param businessOfficeId
	 * @param dataScopeFilter
	 * @return
	 */
	public List<User> getUserListByUserType(String userType, String businessOfficeId, Boolean dataScopeFilter){
		//return userService.findListByUserType(userType, businessOfficeId, dataScopeFilter);
		return null;
	}
	
}